﻿using System.Windows;
using System.Collections.ObjectModel;
using snat.algorithms;
using algorithms;
using System.Collections.Generic;
using System;

namespace gui {
    /// <summary>
    /// Interaction logic for InitialiseBox.xaml.
    /// This box is presented directly after the ConnectBox to allow the
    /// user to register intial algorithms
    /// </summary>
    public partial class InitialiseBox : Window {

        public ObservableCollection<AlgorithmItem> AlgorithmsList { get; set; }

        public InitialiseBox() {
            InitializeComponent();

            // Populate Algorithms List
            try {
                AlgorithmSet.Instance.Init();
            } catch (AlgorithmNameException ex) {
                // If we have a AlgorithmNameException then we cannot proceed, so close the program
                MessageBoxResult answer = MessageBox.Show("Error: Network Model Initialisation Halted.\n\nAdvanced:\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                if (answer == MessageBoxResult.OK) Application.Current.Shutdown();
            }

            AlgorithmsList = new ObservableCollection<AlgorithmItem>();
            foreach (KeyValuePair<String, IAlgorithm> a in AlgorithmSet.Instance) {
                AlgorithmsList.Add(new AlgorithmItem(a.Value, a.Key));
            }
            this.DataContext = this;

            // Make this window the topmost window
            this.Topmost = true;
        }

        private void Init(object sender, RoutedEventArgs e) {
            if (NetworkModelConnect.IsConnected) {

                // Initialise Network Model
                try {
                    NodeFactorySet.Instance.Init();
                } catch (FactoryNameException ex) {
                    // If we have a FactoryNameException then we cannot proceed, so close the program
                    MessageBoxResult answer = MessageBox.Show("Error: Network Model Initialisation Halted.\n\nAdvanced:\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    if (answer == MessageBoxResult.OK) Application.Current.Shutdown();
                }

                // Load Factory Selection Box
                FactorySelectBox box = new FactorySelectBox(AlgorithmsList);
                box.Show();

                this.Close();

            } else {
                // Tape Has Not Loaded therefore we cannot attempt to re-initialise the network model
                MessageBox.Show("Warning : Algorithms can only be registered to run once a database connection has been established and before the dataset has begun playing. Please reinitialise.", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
                ConnectBox box = new ConnectBox();
                box.Show();
                this.Close();
            }
        }

        private void CloseAlgorithmsBox() {
            this.Close();
        }

        private void SelectAll(object sender, RoutedEventArgs e) {
            foreach (AlgorithmItem a in AlgorithmsList) a.Selected = true;
        }

        private void SelectNone(object sender, RoutedEventArgs e) {
            foreach (AlgorithmItem a in AlgorithmsList) a.Selected = false;
        }

    }
}
